import sys

from Communication import *
from IA_player import *
from Map import Map


config={}

class Game(object):
    host=''
    nickname=''
    co=''
    data=[]   
    finished = False
    map_water = []
    
    

        
        # main of the game

    def main(self):
        
        self.ia = IA_player()
        
        # the main need 2 arguments as host and player name.

        if len(sys.argv)==3:
            self.host=sys.argv[1]
            self.nickname=sys.argv[2]
        # In case of no arguments, set default

        elif len(sys.argv)==1:
            self.host='localhost'
            self.nickname='player'
            
        # Can ask help.

        elif len(sys.argv)==2 and sys.argv[1]=='-h'or sys.argv[1]=='--help':
            print 'Please put as arguments the host and your nickname'
            
        # Class main call
        self.co = Communication(self.host, self.nickname,self)
        self.co.connexion()
        self.co.con.sendall('nick %s\n' % (self.nickname))
        self.co.receive_first()
        self.mapturn = Map(int(float(config['rows'])), int(float(config['cols'])))
        
        # tant que le jeu n'est pas fini
        while not self.finished:
            self.do_turn()
        print 'receive final'
        self.co.receive_final()
        print 'co close'
        self.co.con.close()
        
# methode qui appelle les donnees recus par le client.
    def do_turn(self):
        self.co.receive()
        self.mapUpdate()
        print 'Map : '
        self.mapturn.afficher_map()
        print
        print 'Eau de la map :', self.map_water
        self.co.send(self.ia.strategie(self.data, self.mapturn))
        self.data = [] 
        self.mapturn.clear_map()
        
        #recupere les informations concernant la carte en debut de partie
    
    def mapUpdate(self):
        
        for i in self.data :
            data_split = i.split(' ')
            
            ''' If "water" string is on data_split '''
            if(data_split[0] == "water"):
                self.map_water.append(i)
                
            ''' If "entity" string is on data_split '''
            if(data_split[0] == "entity"):
                if(data_split[3]=="0"):
                    self.mapturn.setHuman(int(i.split(' ')[1]),int(i.split(' ')[2]), int(i.split(' ')[3]))
                if(data_split[3]=="1"):
                    self.mapturn.setCop(int(i.split(' ')[1]),int(i.split(' ')[2]), int(i.split(' ')[3]))
                if(data_split[3]=="2"):
                    self.mapturn.setZombie(int(i.split(' ')[1]),int(i.split(' ')[2]), int(i.split(' ')[3]))
                if(data_split[3]=="3"):
                    self.mapturn.setBerserk(int(i.split(' ')[1]),int(i.split(' ')[2]), int(i.split(' ')[3]))
                    
            ''' If "zombie" string is on data_split '''
            if(data_split[0] == "zombie"):   
                    self.mapturn.setOurZombie(int(i.split(' ')[2]),int(i.split(' ')[3]), int(i.split(' ')[1]))
                    
            ''' If "contagion" string is on data_split '''
            if(data_split[0] == "contagion"):
                    self.mapturn.setOurZombie(int(i.split(' ')[3]),int(i.split(' ')[4]), int(i.split(' ')[2]))
                    
        ''' Each turn, we set all the water cell on the map'''
        for j in self.map_water:
                    self.mapturn.setWater(int(j.split(' ')[1]),int(j.split(' ')[2]))

    def infoCarte(self, ligne):
                
        # Creation HashMap configuration
        grandeListe = ligne.split('\n')
        for i in grandeListe :
            instruction = i.split(' ')
            if instruction[0] not in 'end':
                config[instruction[0]]=instruction[1]          
            
        
        '''carte = Map(int(float(config['rows'])), int(float(config['cols'])))
        carte.afficher_map()'''
        

        #recupere les donnees 
        
    def Com_map(self,ligne):
        '''print '> %s \n'%ligne'''
        
        '''grandeListe = ligne.split('\n')
        for i in grandeListe :
            instruction = i.split(' ')
            print instruction[0]
            data.append(i)'''
        for i in ligne.split('\n'):
            self.data.append(i)
            
        
if __name__=="__main__":
    game = Game()
    game.main()